package com.qm.console.service.configSetting.impl;

import com.alibaba.fastjson.JSONObject;
import com.qm.console.component.util.HttpInvokeUtil;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.pubdef.constant.UrlConstant;
import com.qm.console.pubdef.dto.configSetting.ActivityDTO;
import com.qm.console.pubdef.dto.configSetting.AnchorWeightDTO;
import com.qm.console.pubdef.dto.configSetting.PornIdentifyDTO;
import com.qm.console.pubdef.dto.configSetting.WeightDTO;
import com.qm.console.service.configSetting.SettingService;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPut;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SettingServiceImpl implements SettingService {

    @Override
    public BaseRsp listActivities(){
        String url= UrlConstant.ApiServerHost + "/notice/activity/select";
        String body= HttpInvokeUtil.httpGet(url, new HashMap<>(), null);
        BaseRsp rsp= JSONObject.parseObject(body,BaseRsp.class);
        return  rsp;
    }

    @Override
    public BaseRsp updateActivities(ActivityDTO activityDTO){
        String req = JSONObject.toJSONString(activityDTO);
        String url= UrlConstant.ApiServerHost + "/notice/activity/addOrUpdate";
        String body= HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp= JSONObject.parseObject(body, BaseRsp.class);
        return  rsp;
    }

    @Override
    public BaseRsp listPornIdentify(){
        String url= UrlConstant.ApiServerHost + "/check/query/check_image_value";
        String body= HttpInvokeUtil.httpGet(url, new HashMap<>(), null);
        BaseRsp rsp= JSONObject.parseObject(body,BaseRsp.class);
        return  rsp;
    }

    @Override
    public BaseRsp updatePornIdentify(PornIdentifyDTO pornIdentifyDTO){
        String req = JSONObject.toJSONString(pornIdentifyDTO);
        String url=UrlConstant.ApiServerHost + "/check/update/check_image_value";
        String body=HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp=JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp listWeightSetting(){
        String url= UrlConstant.ApiServerHost + "/notice/weight/config";
        String body= HttpInvokeUtil.httpGet(url, new HashMap<>(), null);
        BaseRsp rsp= JSONObject.parseObject(body,BaseRsp.class);
        return  rsp;
    }

    @Override
    public BaseRsp updateWeightSetting(WeightDTO weightDTO){
        String req = JSONObject.toJSONString(weightDTO);
        String url=UrlConstant.ApiServerHost + "/notice/weight/update";
        String body=HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp=JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp listAnchorWeight(){
        String url= UrlConstant.ApiServerHost + "/notice/weight/anchor/list";
        String body= HttpInvokeUtil.httpGet(url, new HashMap<>(), null);
        BaseRsp rsp= JSONObject.parseObject(body,BaseRsp.class);
        return  rsp;
    }

    @Override
    public BaseRsp updateAnchorWeight(AnchorWeightDTO anchorWeightDTO){
        Map<String, Object> paramsMap= new HashMap<>();
        String userIds = StringUtils.join(anchorWeightDTO.getUserIds(), ",");
        paramsMap.put("userIds", userIds);
        paramsMap.put("weightRate", anchorWeightDTO.getWeightRate());
        String req = JSONObject.toJSONString(paramsMap);
        String url=UrlConstant.ApiServerHost + "/notice/weight/anchor/addOrUpdate";
        String body=HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp=JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp DeleteAnchorWeight(String userId){
        JSONObject req=JSONObject.parseObject(userId);
        String url= UrlConstant.ApiServerHost + "/notice/weight/anchor/delete/"+req.get("userId").toString();
        String body=HttpInvokeUtil.httpDelete(url,null);
        BaseRsp rsp=JSONObject.parseObject(body,BaseRsp.class);
        return rsp;
    }

}
